import Vue from 'vue'

const form = {
  namespaced: true,
  state: {
    // 问卷基础对象
    formObjs: {},
    // 问卷当前文件夹
    currentFormFolder: null,
    // 返回url地址 进入问卷设计器模块 返回到不同页面
    backRoute: '/project/my',
    currentPage:1,
    dataShowType:'table',
    uploadStatusMap: {},
    preSelectRandomOption:[]
  },
  mutations: {
    SET_FORM_OBJS: (state, formObjs) => {
      formObjs.forEach((item) => {
        Vue.set(state.formObjs, item.formKey, item)
      })
    },
    SET_CURRENT_FORM_FOLDER: (state, data) => {
      state.currentFormFolder = data
    },
    SET_BACK_ROUTE: (state, route) => {
      state.backRoute = route
    },
    
    SET_CURRENT_PAGE:(state, currentPage) => {
      state.currentPage = currentPage
    },
    SET_DATA_SHOW_TYPE:(state, dataShowType) => {
      state.dataShowType = dataShowType
    },
    SET_UPLOAD_STATUS: (state, statusMap) => {
      state.uploadStatusMap = {...state.uploadStatusMap, ...statusMap}
    },
    SET_PRE_SELECT_RANDOM_OPTION: (state, preSelectRandomOption) => {
      state.preSelectRandomOption = preSelectRandomOption
    }
  },
  actions: {
    setCurrentFormFolder({commit}, data) {
      commit('SET_CURRENT_FORM_FOLDER', data)
    },
    setBackRoute({commit}, route) {
      commit('SET_BACK_ROUTE', route)
    },
    
    setCurrentPage({commit}, currentPage) {
      commit('SET_CURRENT_PAGE', currentPage)
    },
    setDataShowType({commit}, dataShowType) {
      commit('SET_DATA_SHOW_TYPE', dataShowType)
    },
    uploadStatusMap({commit}, status) {
      commit('SET_UPLOAD_STATUS', status)
    },
    uploadPreSelectRandomOption({commit}, data) {
      commit('SET_PRE_SELECT_RANDOM_OPTION', data)
    },
  },
  getters: {
    hasUploading(state) {
      for (const fileId of Object.keys(state.uploadStatusMap)) {
        if (state.uploadStatusMap[fileId] === 1) return true
      }
      return false
    }
  }
}

export default form
